<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>创建考试 - 音乐考试系统</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f7fa;
            min-height: 100vh;
            padding: 20px;
        }
        .container {
            max-width: 1000px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 2px solid #f0f0f0;
        }
        .title {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }
        .form-group {
            margin-bottom: 25px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: bold;
            font-size: 16px;
        }
        input, select, textarea {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e1e1e1;
            border-radius: 8px;
            font-size: 16px;
        }
        input:focus, select:focus, textarea:focus {
            outline: none;
            border-color: #667eea;
        }
        .file-upload {
            border: 2px dashed #667eea;
            border-radius: 8px;
            padding: 30px;
            text-align: center;
            background: #f8f9fa;
            transition: all 0.3s ease;
        }
        .file-upload:hover {
            background: #f0f4ff;
        }
        .file-upload input {
            display: none;
        }
        .file-label {
            display: inline-block;
            padding: 12px 25px;
            background: #667eea;
            color: white;
            border-radius: 8px;
            cursor: pointer;
            font-weight: bold;
            transition: background 0.3s ease;
        }
        .file-label:hover {
            background: #5a6fd8;
        }
        .file-name {
            margin-top: 10px;
            color: #666;
            font-size: 14px;
        }
        .btn {
            display: inline-block;
            padding: 12px 25px;
            background: #667eea;
            color: white;
            text-decoration: none;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-right: 10px;
        }
        .btn:hover {
            background: #5a6fd8;
            transform: translateY(-2px);
        }
        .btn-secondary {
            background: #6c757d;
        }
        .btn-secondary:hover {
            background: #5a6268;
        }
        .error {
            color: #e74c3c;
            text-align: center;
            margin-top: 15px;
            padding: 10px;
            background: #ffeaea;
            border-radius: 5px;
        }
        .action-buttons {
            display: flex;
            justify-content: space-between;
            margin-top: 30px;
        }
        .file-format {
            background: #e8f5e8;
            padding: 20px;
            border-radius: 8px;
            margin-top: 20px;
        }
        .file-format h4 {
            margin-bottom: 10px;
            color: #28a745;
        }
        .file-format table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }
        .file-format th, .file-format td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        .file-format th {
            background: #f8f9fa;
        }
        .creation-method {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }
        .method-tab {
            flex: 1;
            padding: 15px;
            text-align: center;
            background: #f8f9fa;
            border: 2px solid #e1e1e1;
            border-radius: 8px;
            cursor: pointer;
            font-weight: bold;
            transition: all 0.3s ease;
        }
        .method-tab.active {
            background: #667eea;
            color: white;
            border-color: #667eea;
        }
        .method-content {
            display: none;
        }
        .method-content.active {
            display: block;
        }
        .question-item {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 15px;
            border-left: 4px solid #667eea;
        }
        .question-header {
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
        }
        .options-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin: 10px 0;
        }
        .option-item {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .option-label {
            font-weight: bold;
            min-width: 60px;
        }
        .correct-option {
            margin-top: 10px;
        }
        .add-question-btn {
            background: #28a745;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-weight: bold;
            margin-top: 10px;
        }
        .add-question-btn:hover {
            background: #218838;
        }
        .remove-question-btn {
            background: #dc3545;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
            margin-top: 10px;
        }
        .remove-question-btn:hover {
            background: #c82333;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1 class="title">📝 创建新考试</h1>
            <p style="color: #666;">请选择创建方式并填写考试信息</p>
        </div>

        <form method="POST" enctype="multipart/form-data" id="examForm">
            <div class="form-group">
                <label for="title">考试标题</label>
                <input type="text" id="title" name="title" placeholder="请输入考试标题" required>
            </div>

            <div class="form-group">
                <label for="exam_type">考试类型</label>
                <select id="exam_type" name="exam_type" required>
                    <option value="">请选择考试类型</option>
                    <option value="随堂测试">随堂测试</option>
                    <option value="单元测验">单元测验</option>
                    <option value="期中考试">期中考试</option>
                    <option value="期末考试">期末考试</option>
                    <option value="模拟考试">模拟考试</option>
                </select>
            </div>

            <div class="creation-method">
                <div class="method-tab active" data-method="file">📁 文件上传</div>
                <div class="method-tab" data-method="manual">✏️ 手动创建</div>
            </div>

            <!-- 文件上传方式 -->
            <div class="method-content active" id="file-method">
                <div class="form-group">
                    <label>上传题目文件</label>
                    <div class="file-upload">
                        <input type="file" id="exam_file" name="exam_file" accept=".xlsx,.xls,.csv">
                        <label for="exam_file" class="file-label">选择文件</label>
                        <div class="file-name" id="file-name">未选择文件</div>
                        <p style="margin-top: 10px; color: #666; font-size: 14px;">
                            支持格式: Excel (.xlsx, .xls) 或 CSV (.csv)
                        </p>
                    </div>
                </div>

                <div class="file-format">
                    <h4>📋 文件格式说明</h4>
                    <p>请确保您的Excel或CSV文件包含以下列：</p>
                    <table>
                        <thead>
                            <tr>
                                <th>列名</th>
                                <th>说明</th>
                                <th>示例</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>题目</td>
                                <td>题目内容</td>
                                <td>下列哪个音符是C大调的主音？</td>
                            </tr>
                            <tr>
                                <td>选项A</td>
                                <td>第一个选项</td>
                                <td>C</td>
                            </tr>
                            <tr>
                                <td>选项B</td>
                                <td>第二个选项</td>
                                <td>D</td>
                            </tr>
                            <tr>
                                <td>选项C</td>
                                <td>第三个选项</td>
                                <td>E</td>
                            </tr>
                            <tr>
                                <td>选项D</td>
                                <td>第四个选项</td>
                                <td>F</td>
                            </tr>
                            <tr>
                                <td>正确答案</td>
                                <td>正确选项字母 (A/B/C/D)</td>
                                <td>A</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- 手动创建方式 -->
            <div class="method-content" id="manual-method">
                <div class="form-group">
                    <label>题目数量</label>
                    <input type="number" id="question_count" name="question_count" min="1" max="50" value="1" onchange="updateQuestionForms()">
                </div>

                <div id="questions-container">
                    <!-- 题目表单将通过JavaScript动态生成 -->
                </div>

                <button type="button" class="add-question-btn" onclick="addQuestion()">+ 添加题目</button>
            </div>

            {% if error %}
            <div class="error">{{ error }}</div>
            {% endif %}

            <div class="action-buttons">
                <a href="/dashboard" class="btn btn-secondary">返回主页</a>
                <button type="submit" class="btn">创建考试</button>
            </div>
        </form>
    </div>

    <script>
        // 文件选择显示
        document.getElementById('exam_file').addEventListener('change', function(e) {
            const fileName = e.target.files[0] ? e.target.files[0].name : '未选择文件';
            document.getElementById('file-name').textContent = fileName;
        });

        // 切换创建方式
        document.querySelectorAll('.method-tab').forEach(tab => {
            tab.addEventListener('click', function() {
                // 移除所有active类
                document.querySelectorAll('.method-tab').forEach(t => t.classList.remove('active'));
                document.querySelectorAll('.method-content').forEach(c => c.classList.remove('active'));
                
                // 添加active类到当前选项卡
                this.classList.add('active');
                const method = this.getAttribute('data-method');
                document.getElementById(method + '-method').classList.add('active');
            });
        });

        let questionCount = 1;

        function updateQuestionForms() {
            const count = parseInt(document.getElementById('question_count').value);
            const container = document.getElementById('questions-container');
            
            // 确保count在合理范围内
            if (count < 1) count = 1;
            if (count > 50) count = 50;
            
            // 更新题目数量
            questionCount = count;
            
            // 重新生成题目表单
            container.innerHTML = '';
            for (let i = 0; i < count; i++) {
                addQuestionForm(i);
            }
        }

        function addQuestion() {
            const count = parseInt(document.getElementById('question_count').value);
            document.getElementById('question_count').value = count + 1;
            updateQuestionForms();
        }

        function addQuestionForm(index) {
            const container = document.getElementById('questions-container');
            const questionDiv = document.createElement('div');
            questionDiv.className = 'question-item';
            questionDiv.innerHTML = `
                <div class="question-header">题目 ${index + 1}</div>
                <div class="form-group">
                    <label>题目内容</label>
                    <textarea name="question_${index}" placeholder="请输入题目内容..." required></textarea>
                </div>
                <div class="options-grid">
                    <div class="option-item">
                        <span class="option-label">选项A:</span>
                        <input type="text" name="option_a_${index}" placeholder="选项A内容" required>
                    </div>
                    <div class="option-item">
                        <span class="option-label">选项B:</span>
                        <input type="text" name="option_b_${index}" placeholder="选项B内容" required>
                    </div>
                    <div class="option-item">
                        <span class="option-label">选项C:</span>
                        <input type="text" name="option_c_${index}" placeholder="选项C内容" required>
                    </div>
                    <div class="option-item">
                        <span class="option-label">选项D:</span>
                        <input type="text" name="option_d_${index}" placeholder="选项D内容" required>
                    </div>
                </div>
                <div class="form-group correct-option">
                    <label>正确答案</label>
                    <select name="correct_${index}" required>
                        <option value="">请选择正确答案</option>
                        <option value="A">A</option>
                        <option value="B">B</option>
                        <option value="C">C</option>
                        <option value="D">D</option>
                    </select>
                </div>
                ${index > 0 ? `<button type="button" class="remove-question-btn" onclick="removeQuestion(${index})">删除此题</button>` : ''}
            `;
            container.appendChild(questionDiv);
        }

        function removeQuestion(index) {
            const count = parseInt(document.getElementById('question_count').value);
            document.getElementById('question_count').value = count - 1;
            updateQuestionForms();
        }

        // 初始化题目表单
        updateQuestionForms();
    </script>
</body>
</html>